
package com.tanhua.server.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.service.MessagesService;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/management/manage/messages")
public class MessagesController {


    @Autowired
    private MessagesService messagesService;

    //消息通过
    @PostMapping("/pass")
    public ResponseEntity<String> messagesPass(@RequestParam(value = "Authorization",required = false) String token,
                                               @RequestBody String[] ids) {
        try {
            Boolean boo = messagesService.messagesPass(ids);
            if (!boo) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok("操作成功");
    }

    //消息拒绝
    @PostMapping("/reject")
    public ResponseEntity<String> messagesrReject(@RequestParam(value = "Authorization",required = false) String token,
                                                  @RequestBody String[] ids) throws IOException {
        try {
            Boolean boo = messagesService.messagesReject(ids);
            if (!boo) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok("操作成功");
    }

    //撤销
    @PostMapping("/revocation")
    public ResponseEntity<Map<String,String>> messagesRevocation(@RequestParam(value = "Authorization",required = false) String token,
                                                     @RequestBody String[] ids) {
        try {
            Boolean boo = messagesService.messagesRevocation(ids);
            if (!boo) {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        String messgae = "操作成功";
        Map<String, String> map = new HashMap<>();
        map.put("messgae",messgae);
        return ResponseEntity.ok(map);
    }


    //消息翻页
    @GetMapping
    //@RequestParam("Authorization")String token
    public PageResult queryMessages(@RequestParam(value = "Authorization",required = false) String token,
                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "sortProp") String sortProp,//排序字段
                                    @RequestParam(value = "sortOrder", defaultValue = "1") String sortOrder,//排序规则
                                    @RequestParam(value = "id", required = false) String id, //消息id
                                    @RequestParam(value = "sd", required = false) String sd,  //开始时间
                                    @RequestParam(value = "ed", required = false) String ed,   //结束时间
                                    @RequestParam(value = "uid",required = false) String userId,  //用户id
                                    @RequestParam(value = "state",required = false) String state) {


        PageResult pageResult = null;
        try {
            pageResult = messagesService.queryMessages(page, pageSize, sortProp, sortOrder, id, sd, ed, userId, state);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        System.out.println(pageResult.toString());
        return pageResult;
    }

    //置顶
    @PostMapping("/{id}/top")
    public String topMessage(@RequestParam(value = "Authorization",required = false) String token,
                             @PathVariable(value = "id") String id) {         //
        try {
            Boolean boo = messagesService.topMessage(id);
            if (boo) {
                return "置顶成功";
            }
            return "置顶失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "置顶失败";
        }
    }

    @PostMapping("/{id}/untop")
    public String untopMessage(@RequestParam(value = "Authorization",required = false) String token,
                               @PathVariable(value = "id") String id) {         //
        try {
            Boolean boo = messagesService.unTopMessage(id);
            if (boo) {
                return "取消置顶成功";
            }
            return "取消置顶失败";
        } catch (Exception e) {
            e.printStackTrace();
            return "取消置顶失败";
        }
    }

}

